const { exec } = require("../db/mysql");


const addNews = (newsData = {}) => {
  const news_title = newsData.news_title;
  const news_content = newsData.news_content;
  const news_time = new Date().getTime();
  const sql = `insert into news (news_title,news_content,news_time) 
    values('${news_title}','${news_content}','${news_time}');`;

  return exec(sql).then((insertData) => {
    if (insertData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const getNewsList = () => {
  const sql = `select * from news`;
  return exec(sql);
};
const delNews = (id) => {
  const sql = `delete from news where news_id=${id}`;
  return exec(sql).then((delData) => {
    if (delData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const updateNews = (id, newsData = {}) => {
  let news_title = newsData.news_title;
  let news_content = newsData.news_content;
  let news_time = new Date().getTime()

  const sql = `update news set news_title='${news_title}',news_content='${news_content}' ,
  news_time='${news_time}' where news_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};

module.exports = {
  addNews,
  getNewsList,
  delNews,
  updateNews,
};
